package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.FileUtil;
import com.isunland.managesystem.utils.LogUtil;
import java.util.Date;

public abstract class EmployeePaymentFragment extends Fragment implements OnClickListener {

  private static final int REQUEST_REPAYMENTDATE = 0;
  private static final int REQUEST_REPAYMENSTYLE = 1;
  private static final int REQUEST_ADDEXTRA = 2;//附件
  protected static final int REQUEST_RENAME = 3;//重命名
  protected static final int SAVE = 4;//添加保存
  protected static final int SAVE_START = 5;//添加 保存并启动流程
  protected BaseVolleyActivity mActivity;
  protected CurrentUser mCurrentUser;
  protected String mrepayName;
  protected String mrepayId;
  protected TextView mCertificateNoRepayment;//单据编号
  protected EditText mExpenseDescRepayment;//费用摘要

  protected EditText mPoutAmountRepayment;//还款金额
  protected TextView mSumBorrowRepayment;//当前借款合计

  protected TextView mLoanPersonRepayment;//还款人
  protected TextView mLoanProposerRepayment;//申报人
  protected TextView mLoanDateRepayment;//申报日期
  protected TextView mRepayDateRepayment;//还款日期
  protected ImageView mIbRepayDateRepayment;
  protected TextView mRepayTypeRepayment;//还款方式

  protected ImageView mIbRepayTypeRepayment;

  //	protected TextView mAccessoryRepayment;//附件资料
  //	protected ImageButton mIbAccessoryRepayment;
  protected TextView mTvExtra;//附件
  protected ImageView mIvExtra;//附件

  //开户行
  protected EditText etpayeeBank;//开户行
  protected EditText etpayeeAccname;//账户
  protected EditText etpayeeAccount;//账号
  protected String selcurFile = "";//文件上传服务器返回路径
  protected String mFilePath = "";//文件本地路径
  protected String id = "";
  protected String mfileName = "";

  public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mActivity = (BaseVolleyActivity) getActivity();
    mCurrentUser = CurrentUser.newInstance(getActivity());
    setTitle();

    super.onCreate(savedInstanceState);
  }

  /**
   * 设置标题
   */
  protected abstract void setTitle();

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_employee_repayment, container, false);
    mCertificateNoRepayment = (TextView) view.findViewById(R.id.tv_certificateNo_repayment);
    mExpenseDescRepayment = (EditText) view.findViewById(R.id.et_expenseDesc_repayment);
    mPoutAmountRepayment = (EditText) view.findViewById(R.id.et_poutAmount_repayment);
    mSumBorrowRepayment = (TextView) view.findViewById(R.id.tv_sumBorrow_repayment);
    mLoanPersonRepayment = (TextView) view.findViewById(R.id.tv_loanPerson_repayment);
    mLoanProposerRepayment = (TextView) view.findViewById(R.id.tv_loanProposer_repayment);
    mLoanDateRepayment = (TextView) view.findViewById(R.id.tv_loanDate_repayment);
    mRepayDateRepayment = (TextView) view.findViewById(R.id.tv_repayDate_repayment);
    mIbRepayDateRepayment = (ImageView) view.findViewById(R.id.ib_repayDate_repayment);
    mRepayTypeRepayment = (TextView) view.findViewById(R.id.tv_repayType_repayment);
    mIbRepayTypeRepayment = (ImageView) view.findViewById(R.id.ib_repayType_repayment);

    mIbRepayDateRepayment.setOnClickListener(this);
    mIbRepayTypeRepayment.setOnClickListener(this);

    //开户行
    etpayeeBank = (EditText) view.findViewById(R.id.et_payeeBank);
    etpayeeAccname = (EditText) view.findViewById(R.id.et_payeeAccname);
    etpayeeAccount = (EditText) view.findViewById(R.id.et_payeeAccount);
    //附件
    mTvExtra = (TextView) view.findViewById(R.id.tv_extra);
    mIvExtra = (ImageView) view.findViewById(R.id.iv_extra);
    mIvExtra.setOnClickListener(this);
    initData();
    return view;
  }

  /**
   * 初始化数据
   */
  protected void initData() {

  }

  @Override public void onClick(View arg0) {
    switch (arg0.getId()) {

      // 还款日期
      case R.id.ib_repayDate_repayment:
        showDialog(REQUEST_REPAYMENTDATE, new Date());
        break;

      // 还款方式
      case R.id.ib_repayType_repayment:
        showDialog(REQUEST_REPAYMENSTYLE, null);
        break;
      // 添加附件
      case R.id.iv_extra:
        showDialog(REQUEST_ADDEXTRA, null);
        break;

      default:
        break;
    }
  }

  private void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;
    switch (requestCode) {
      case REQUEST_REPAYMENTDATE:
        dialog = WorkProgressTimeDialogFragment.newInstance(date, R.string.choose_repaytDate);
        break;
      case REQUEST_REPAYMENSTYLE:
        dialog = new RepayTypeDialogFragment();
        break;
      case REQUEST_ADDEXTRA:
        dialog = ExtraUpLoadDialogFragment.newInstance(mFilePath);
        break;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(EmployeePaymentFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    // 还款日期
    if (requestCode == REQUEST_REPAYMENTDATE && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      String mRepayDate = DateUtil.dateToString(acquireDate);
      mRepayDateRepayment.setText(mRepayDate);

      return;
    }
    // 还款方式
    if (requestCode == REQUEST_REPAYMENSTYLE && data != null) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mrepayName = customer.getName();
      mrepayId = customer.getId();
      mRepayTypeRepayment.setText(mrepayName);//还款方式

      return;
    }
    //附件
    if (requestCode == REQUEST_ADDEXTRA) {
      String result = data.getStringExtra(ExtraUpLoadDialogFragment.EXTRA_RESULT);
      if (ExtraUpLoadDialogFragment.VALUE_CLEAR.equalsIgnoreCase(result)) {
        clearExtraFile();
      } else {

        mFilePath = result;
        LogUtil.i("mDocPath===" + mFilePath);
        if (mFilePath != null) {
          mfileName = FileUtil.getFileNameByFilePath(mFilePath);
          //					String fileName = FileUtil.getFileNameByFilePath(mDocPath);
          //					String[] names =fileName.split("\\.");
          //					mfileName =names[names.length-2]+"."+names[names.length-1];
          mTvExtra.setText(mfileName);
        }
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public void clearExtraFile() {
    mFilePath = "";
    mfileName = "";
    mTvExtra.setText("");
  }
}
